package org.beynet.jsonbinding.parser.types.impl;

import java.io.IOException;
import java.io.InputStream;

import org.beynet.jsonbinding.parser.JSONParsingException;
import org.beynet.jsonbinding.parser.JSONParsingExceptionUnexpectedChar;
import org.beynet.jsonbinding.parser.types.JSONParser;

/**
 * Entry point to parse JSON.
 * @author beynet
 *
 */
public class JSONParserImpl implements JSONParser {
    public JSONParserImpl() {
        
    }
    @Override
    public Object parse(String encoding,InputStream is) throws IOException, JSONParsingException {
        return initParsing(encoding,is);
    }
    protected Object initParsing(String encoding,InputStream is) throws IOException, JSONParsingException {
        int r = 0;
        while (true) {
            r = is.read();
            if (r == ' ' || r == '\t' || r == '\r' || r == '\n') {
                continue;
            } else if (r == '{' ) {
                return new JSONObjectImpl(encoding,is);
            } else if (r == '[' ) {
                return new JSONArrayImpl(encoding,is);
            }
            else {
                throw new JSONParsingExceptionUnexpectedChar(r);
            }
        }
    }
}
